package com.jxc.summer.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jxc.summer.dao.CableMapper;
import com.jxc.summer.service.CableService;

@Service
public class CableServiceImpl implements CableService {

	@Autowired
	private CableMapper cableMapper;
	
	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED, readOnly=true)
	@Override
	public Map<String, Object> pageList(Map<String, Object> params) {
		List<Map<String, Object>> list = cableMapper.pageList(params);
		int count = cableMapper.pageListCount(params);
		// package result
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("recordsTotal", count);		// 总记录数
		maps.put("recordsFiltered", count);	// 过滤后的总记录数
		maps.put("data", list); 
		return maps;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
	@Override
	public int addCable(Set<Map<String, Object>> cablestandardList) {
		int result = 0;
		for (Map<String, Object> map : cablestandardList) {
			//查看数据库中是否有重复的数据如果有continue
			if(cableMapper.validateCable(map)>0){
				continue;
			}
			int r = cableMapper.addCable(map);
			result += r;
		}
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
	@Override
	public int updateCable(Map<String, Object> params) {
		int result = cableMapper.updateCable(params);
		return result;
	}

	@Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.READ_COMMITTED)
	@Override
	public int deleteCable(Map<String, Object> params) {
		int result = cableMapper.deleteCable(params);
		return result;
	}

	@Override
	public int deleteBymodel(Map<String, Object> params) {
		int result = cableMapper.deleteCableByModel(params);
		return result;
	}

	@Override
	public List<String> getModel(String companyCode) {
		return cableMapper.getModel(companyCode);
	}

	@Override
	public List<String> getstandard(String model, String companyCode) {
		return cableMapper.getstandard(model,companyCode);
	}

}
